package com.mazaiting.instructions.conversions;

import com.mazaiting.instructions.base.NoOperandsInstruction;
import com.mazaiting.rtda.MtFrame;
import com.mazaiting.rtda.MtOperandStack;

/**
 * f2x系列指令把float变量强制转换成其他类型
 *
 * @author mazaiting
 * @since 2021/8/15
 */
public class F2xInstruction {

    /**
     * 单精度转换为双精度
     */
    public static class F2D extends NoOperandsInstruction {
        @Override
        public void execute(MtFrame frame) {
            MtOperandStack stack = frame.getOperandStack();
            stack.pushDouble(stack.popFloat());
        }

        @Override
        public String toString() {
            return "f2d";
        }
    }

    /**
     * 单精度转换为整型
     */
    public static class F2I extends NoOperandsInstruction {
        @Override
        public void execute(MtFrame frame) {
            MtOperandStack stack = frame.getOperandStack();
            float v1 = stack.popFloat();
            int v2 = (int) v1;
            stack.pushInt(v2);
        }

        @Override
        public String toString() {
            return "f2i";
        }
    }

    /**
     * 单精度转换为长整型
     */
    public static class F2L extends NoOperandsInstruction {
        @Override
        public void execute(MtFrame frame) {
            MtOperandStack stack = frame.getOperandStack();
            float v1 = stack.popFloat();
            long v2 = (long) v1;
            stack.pushLong(v2);
        }

        @Override
        public String toString() {
            return "f2l";
        }
    }
}
